package de.pyranja.timing;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Strings.emptyToNull;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.joda.time.DateTime;

import com.google.common.base.Ticker;

/**
 * A container for the measurement of query execution. Note that this implementation
 * is not thread-safe and allows already set markers to be overwritten.
 * @author Chris Borckholder
 */
public class Measurement {
	
	// time point references
	public static final String START = "MEASUREMENT_START";
	public static final String END = "MEASUREMENT_END";
	
	// attributes
	private final String id;
	private final DateTime created;
	private final String description;
	
	// dependency
	private final Map<String,Moment> points;
	private final Ticker ticker;
	
	/**
	 * Constructs a new measurement for specified query. Does not start the 
	 * measurement.
	 * @param query that will be measured
	 */
	public Measurement(String id, String description, Ticker ticker) {
		super();
		this.id = checkNotNull(emptyToNull(id),"ID cannot be empty");
		this.description = checkNotNull(emptyToNull(description),"description cannot be empty");
		this.created = DateTime.now();
		this.points = new HashMap<String,Moment>();
		this.ticker = ticker;
	}

	/**
	 * Starts the measurement and adds the current time point as START
	 * @return the current time point
	 */
	public Moment start() {
		return this.mark(START);
	}
	
	/**
	 * Ends the measurement and adds the current time point as END
	 * @return the current time point
	 */
	public Moment end() {
		return this.mark(END);
	}
	
	/**
	 * Marks a point in time with the given title
	 * @param title of the time point
	 * @return current time point
	 */
	public Moment mark(String title) {
		checkNotNull(emptyToNull(title), "Cannot use empty marker");
		final long now = ticker.read();
		final Moment moment = new Moment(now,title); 
		points.put(title,moment);
		return moment;
	}
	
	/**
	 * Retrieves the time point that was marked with the specified title
	 * @param title of time point
	 * @return time point marked with specified title
	 * @throws IllegalArgumentException if no time point with that title is found
	 */
	public Moment get(String title) {
		final Moment result = points.get(title);
		if (result != null) {
			return result;
		} else {
			throw new IllegalArgumentException("Marker "+ title +" is not set");
		}
	}
	
	/**
	 * Retrieves all recorded moments in this measurement, ordered by their time.
	 * @return an ordered list of moments
	 */
	public List<Moment> moments() {
		final List<Moment> result = new ArrayList<Moment>(points.values());
		Collections.sort(result);
		return result;
	}

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	/**
	 * @return the created
	 */
	public DateTime getCreated() {
		return created;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Measurement [id=" + id + ", created=" + created
				+ ", description=" + description + ", points=" + points
				+ ", ticker=" + ticker + "]";
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Measurement other = (Measurement) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
}
